% Script to convert Patrick's SPH coefficients from text to COF format


nmax = 20;
wmmfixed = load('/Users/manojnair/projects/wmm2015_validation/WMM2015_fixed.txt');  

L = wmmfixed(:,2) <= 0;

nm = wmmfixed(L,2);
hnm = wmmfixed(L,3);
hnmsv = wmmfixed(L,4);
L = nm == 0;
hnm(L) = 0;
hnmsv(L) = 0;

L = wmmfixed(:,2) >= 0;
gnm = wmmfixed(L,3);
gnmsv = wmmfixed(L,4);



%% Now thw reverse (i.e. from COF to TEXT)

wmmcof = load('/Users/manojnair/projects/wmm2015_validation/WMM2015_FINAL.cof.txt');

nmax = 12;

nrow = 90;
%nrow = 44;

ab = wmmcof(:,1:2);
ab(end+1:end+nrow,:) = [ wmmcof(:,1) -wmmcof(:,2)];
mcoeff(1:nrow,:) =  wmmcof(:,3);
mcoeff(nrow+1:2*nrow,:) =  wmmcof(:,4);
svcoeff(1:nrow,:) =  wmmcof(:,5);
svcoeff(nrow+1:2*nrow,:) =  wmmcof(:,6);

nmax = 12;

fname = '/Users/manojnair/projects/wmm2015_validation/WMM2015_FINAL.txt';
fid = fopen(fname,'wt');

% Magnetic field model coefficients
% nmax:  20
% epoch: 2015.0000
% radius: 6371.2
%   n     m          MF gnm (nT)     SV gnm (nT/year)   SA gnm (nT/year^2)

fprintf(fid,' Magnetic field model coefficients\n');
fprintf(fid,' nmax:  20\n');
fprintf(fid,' epoch: 2015.0000\n');
fprintf(fid,' radius: 6371.2\n');
fprintf(fid,'   n     m          SV gnm (nT)    SV gnm (nT/year)   SA gnm (nT/year^2) \n');

for i = 1:nmax,
    
    for j = -i:i,
        
        k = find(ab(:,1)==i & ab(:,2) == j);
        
        fprintf(fid,'   %4d    %4d    %12.4f     %12.4f     %12.4f\n', i, j, mcoeff(k(1)), svcoeff(k(1)),0);
    end;
end;

fclose(fid);

%% Now thw reverse (i.e. from COF to TEXT)

wmmcof = load('/Users/manojnair/projects/igrf12_validation/igrf11.cof.txt');

nmax = 8;

%nrow = 90;
nrow = 44;

ab = wmmcof(:,1:2);
ab(end+1:end+nrow,:) = [ wmmcof(:,1) -wmmcof(:,2)];
mcoeff(1:nrow,:) =  wmmcof(:,3);
mcoeff(nrow+1:2*nrow,:) =  wmmcof(:,4);
svcoeff(1:nrow,:) =  wmmcof(:,5);
svcoeff(nrow+1:2*nrow,:) =  wmmcof(:,6);

nmax = 8;

fname = '/Users/manojnair/projects/igrf12_validation/igrf11.txt';
fid = fopen(fname,'wt');

% Magnetic field model coefficients
% nmax:  20
% epoch: 2015.0000
% radius: 6371.2
%   n     m          MF gnm (nT)     SV gnm (nT/year)   SA gnm (nT/year^2)

fprintf(fid,' Magnetic field model coefficients\n');
fprintf(fid,' nmax:  20\n');
fprintf(fid,' epoch: 2015.0000\n');
fprintf(fid,' radius: 6371.2\n');
fprintf(fid,'   n     m          SV gnm (nT)     Filler    Filler \n');

for i = 1:nmax,
    
    for j = -i:i,
        
        k = find(ab(:,1)==i & ab(:,2) == j);
        
        fprintf(fid,'   %4d    %4d    %12.4f     %12.4f     %12.4f\n', i, j, svcoeff(k(1)), 0,0);
    end;
end;

fclose(fid);


%%
% Replace the Main column with SV in a text file

data = load('/Users/manojnair/projects/emm2015_validation/emm2015_a.txt');


fname = '/Users/manojnair/projects/emm2015_validation/reformatted/emm_2015_a.txt';
fid = fopen(fname,'wt');

% Magnetic field model coefficients
% nmax:  20
% epoch: 2014.0000
% radius: 6371.2
%   n     m          MF gnm (nT)     SV gnm (nT/year)   SA gnm (nT/year^2)

fprintf(fid,'%% Magnetic field model coefficients\n');
fprintf(fid,'%% nmax:  15\n');
fprintf(fid,'%% epoch: 2015.0000\n');
fprintf(fid,'%% radius: 6371.2\n');
fprintf(fid,'%%   n     m          SV gnm (nT)     Filler    Filler \n');

for i = 1:length(data),
    
            
        fprintf(fid,'   %4d    %4d    %12.4f     %12.4f     %12.4f\n', data(i,1), data(i,2), data(i,4), 0,0);
   
end;

fclose(fid);
